package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.PublicAuditListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.PackageDetailListEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.Collections;

import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class MaterielBoxCheckActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etJobNo)
    FreeUI_ClearEditText etJobNo;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.etReelID)
    FreeUI_ClearEditText etReelID;
    @BindView(R.id.tvAdd)
    TextView tvAdd;
    @BindView(R.id.rvMaterielList)
    RecyclerView rvMaterielList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvQty)
    TextView tvQty;
    @BindView(R.id.tvMainQty)
    TextView tvMainQty;
    private PublicAuditListAdapter mPublicAuditListAdapter;
    private ArrayList<PackageDetailListEntity> pdlist = new ArrayList<PackageDetailListEntity>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_materiel_box_check;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setVisibility(View.GONE);
        setMainLayout(llMain);
        initRecyclerView(rvMaterielList, this);
        mPublicAuditListAdapter = new PublicAuditListAdapter(null, true,"");
        mPublicAuditListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mPublicAuditListAdapter.isFirstOnly(true);
        rvMaterielList.setAdapter(mPublicAuditListAdapter);

        initEdit(etReelID, s -> {

            if (getText(etLotID).equals("")){
                showToast("批次ID不能为空", false);
                return;
            }

            FreeUI_EntityCallBack<BaseResponseBean<PackageDetailListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PackageDetailListEntity>>
                    (new TypeToken<BaseResponseBean<PackageDetailListEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(MaterielBoxCheckActivity.this, false, getSupportFragmentManager())) {
                        PackageDetailListEntity body = response.body().getResponse().getBody();
                        if ("REEL".equals(body.getXpackagedetail().getPackageType())) {
                            publicAudit(getSupportFragmentManager(), getText(etLotID), s, "");
                        } else {
                            showToast("请输入正确的卷盘ID", false);
                            etReelID.setText("");
                            etReelID.requestFocus();
                        }
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                    super.onError(response);

                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTION_TYPE", "QUERY");
            map.addProperty("CHECKPACKLOTID", s);
            map.addProperty("LOT_ID", getText(etLotID));
            NetUtils.requestPostNet("IF.LBLAPPROVE", "Common", MaterielBoxCheckActivity.this, map, callBack);


//            @Override
//            public void onTextChangeListener(String s) {
//                if (getText(etLotID).equals("")){
//                    showToast("请先扫描批次ID",false);
//                    etReelID.setText("");
//                    return;
//                }
//
//                LotUtils.publicAudit(MaterielBoxCheckActivity.this, getSupportFragmentManager(),  getText(etLotID), getText(etReelID),"REEL", new LotUtils.netCallback<PackageDetailListEntity>() {
//                    @Override
//                    public void onSuccess(PackageDetailListEntity response) {
//                        if (isHavePackID(response)){
//                            pdlist.clear();
//                            pdlist.add(response);
//                            Collections.reverse(pdlist);
//                            mPublicAuditListAdapter.addData(pdlist);
//                            etReelID.setText("");
//                            etReelID.requestFocus();
////                            publicAudit(getSupportFragmentManager(),  getText(etLotID), getText(etReelID),);
//
//                        }
//                    }
//
//                    @Override
//                    public void onFail(Throwable t) {
//                        loadError(t,"PUBLICAUDIT");
//                    }
//                });
//            }
        });
    }

    private void publicAudit(FragmentManager fragmentManager, String lotID, String packLotID, String parentpacklotid) {
        FreeUI_EntityCallBack<BaseResponseBean<PackageDetailListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PackageDetailListEntity>>
                (new TypeToken<BaseResponseBean<PackageDetailListEntity>>() {
                }.getType(), fragmentManager, this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(MaterielBoxCheckActivity.this, false, fragmentManager)) {
                    FreeApi_ToastUtils.showFreeToast("核对成功", MaterielBoxCheckActivity.this, true, 0);
//                    pdlist.add(response.body().getResponse().getBody());
//                    Collections.reverse(pdlist);
                    PackageDetailListEntity packageDetailListEntity =
                            new PackageDetailListEntity(getText(etReelID), response.body().getResponse().getBody().getXpackagedetail().getPackMainQty()+"");

                    mPublicAuditListAdapter.addData(0,packageDetailListEntity);
                    etReelID.setText("");
                    etReelID.requestFocus();
                    tvQty.setText(mPublicAuditListAdapter.getData().size()+"");
                    int i = Integer.parseInt(tvMainQty.getText().toString());
                    for (PackageDetailListEntity data:mPublicAuditListAdapter.getData()) {
                        i += Integer.parseInt(data.getPACKLOTID().equals("")?"0":data.getPACKLOTID());
                    }
                    tvMainQty.setText(i+"");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                super.onError(response);
                FreeApi_LogUtils.saveErrorMessages(response.getException(), "PUBLICAUDIT");
                FreeApi_ToastUtils.showFreeToast("连接服务器失败", MaterielBoxCheckActivity.this, false, 0);

            }

        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTION_TYPE", "APPROVE");
        map.addProperty("LOT_ID", lotID);
        map.addProperty("CHECKPACKLOTID", packLotID);
        map.addProperty("CHECKTYPE", "reelCheck");
        map.addProperty("PARENTPACKLOTID", parentpacklotid);
        NetUtils.requestPostNet("IF.LBLAPPROVE", "Common", MaterielBoxCheckActivity.this, map, callBack);

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.tvBaseSubmit:
////                if (actionControl("Submit12"))return;
//                if (mPublicAuditListAdapter.getData().size() <= 0){
//                    showToast("请先添加核对信息",false);
//                    return;
//                }
//
//                break;
//            case R.id.tvAdd:
//                if (getText(etLotID).equals("") || getText(etReelID).equals("")){
//                    showToast("批次号或卷盘ID不能为空",false);
//                    return;
//                }
//
//                LotUtils.publicAudit(this, getSupportFragmentManager(),  getText(etLotID), getText(etReelID),"REEL", new LotUtils.netCallback<PackageDetailListEntity>() {
//                    @Override
//                    public void onSuccess(PackageDetailListEntity response) {
//                        if (isHavePackID(response)){
//                            mPublicAuditListAdapter.addData(response);
//                            etLotID.setText("");
//                            etReelID.setText("");
//                        }
//                    }
//
//                    @Override
//                    public void onFail(Throwable t) {
//                        loadError(t,"PUBLICAUDIT");
//                    }
//                });
//                break;
        }

    }

    private boolean isHavePackID(PackageDetailListEntity response) {
        if (mPublicAuditListAdapter.getData().size() <= 0) {
            return true;
        }

        for (PackageDetailListEntity entity : mPublicAuditListAdapter.getData()) {
            if (entity.getPACKLOTID().equals(response.getPACKLOTID())) {
                showToast("当前包装条码已在列表中", true);
                return false;
            }
        }

        return true;
    }


}
